﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using RTSoft.SoureCode.Control.Luong;
using RTSoft.Modules;

namespace RTSoft.SoureCode.Entity
{
    public partial class frmLuong : DevExpress.XtraEditors.XtraForm
    {
        public frmLuong()
        {
            InitializeComponent();
        }
        DataTable tbl;
        String errSQL;
        String errConnect;
        private void frmLuong_Load(object sender, EventArgs e)
        {
            try
            {
                this.InitGrid();
            }
            catch
            {
                Library.ShowMessage("Lỗi dữ liệu đầu vào!");
            }
        }

        public void InitGrid()
        {
            tbl = new DataTable();
            errConnect = "";
            errSQL = "";
            luongctrl ctrl = new luongctrl();
            txtSalary.Text = ctrl.getSalary(ref errConnect, ref errSQL).ToString("###,###,### VNĐ");
            txtSalary.Properties.ReadOnly = true;
            txtDate.Properties.ReadOnly = true;
            txtDate.Text = ctrl.getDate(ref errConnect, ref errSQL);
            ctrl.getData(ref errConnect, ref errSQL, ref tbl);
            GridControl1.DataSource = tbl;
            GridView1.Columns["TEN_NV"].Caption = "Tên nhân viên";
            GridView1.Columns["LUONG_CB"].Caption = "Lương";
            GridView1.Columns["LUONG_CB"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            GridView1.Columns["LUONG_CB"].DisplayFormat.FormatString = "###,###,##0";
            GridView1.Columns["BAC"].Caption = "Trình độ";
            GridView1.Columns["HESO"].Caption = "Hệ số lương";
            GridView1.BestFitColumns();
            GridView1.OptionsBehavior.Editable = false;
        }
        #region "EventButton"
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            //if (GridView1.FocusedRowHandle >= 0)
            //{
            //    int id = Convert.ToInt32(GridView1.GetRowCellValue(GridView1.FocusedRowHandle, GridView1.Columns["ID_LUONG"].FieldName));
            //    errConnect = "";
            //    errSQL = "";
            //    string strName = GridView1.GetRowCellValue(GridView1.FocusedRowHandle, GridView1.Columns["LUONG_CB"].FieldName).ToString();
            //    Boolean result = Library.ShowQuestionMessage("Bạn có chắc chắn xóa " + strName + " ?");
            //    if (result == false)
            //    {
            //        return;
            //    }
            //    luongctrl ctrl = new luongctrl();
            //    ctrl.Delete(ref errConnect, ref errSQL, id);
            //    GridView1.DeleteSelectedRows();
            //    GridView1.RefreshData();
            //}
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            frmLuong frm = new frmLuong();
            try
            {
                Library.ExportToExcel(frm, "Lương", GridView1);
            }
            catch {
                Library.ShowMessage("Lỗi!");
            }
        }
        #endregion


        // Thêm sửa lương
        private void GridView1_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            //luongctrl ctrl = new luongctrl();
            //luonginfo info = new luonginfo();
                
            //errConnect = "";
            //errSQL = "";
            //DataRow row = GridView1.GetDataRow(e.RowHandle);
            //info.ID_LUONG = Convert.ToInt32(RTValidate.CheckNull(row["ID_LUONG"]));
            //info.LUONG_CB = Convert.ToInt32(RTValidate.CheckNull(row["LUONG_CB"]));
            //if (info.ID_LUONG != 0)
            //{
            //    Boolean result = Library.ShowQuestionMessage("Dữ liệu có sự thay đổi bạn có chắc sửa");
            //    if (result != false)
            //    {
            //        ctrl.Update(ref errConnect, ref errSQL, info);
            //    }
            //}
            //else
            //{
            //    ctrl.Insert(ref errConnect, ref errSQL, info);
            //}
            //this.InitGrid();   
        }

        private void txtSalary_DoubleClick(object sender, EventArgs e)
        {
            if (Library.ShowQuestionMessage("Bạn có chắc muốn đổi mức lương cơ bản?"))
            {
                txtSalary.Properties.ReadOnly = false;
                txtSalary.Focus();
                txtSalary.SelectAll();
            }
        }

        private void txtSalary_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) {
                luongctrl ctrl = new luongctrl();
                luonginfo info = new luonginfo();

                errConnect = "";
                errSQL = "";     
                info.LUONG_CB = Convert.ToInt32(RTValidate.CheckNull(txtSalary.EditValue));
                info.NGAY = DateTime.Now;
                ctrl.Insert(ref errConnect, ref errSQL, info);
                Library.ShowMessage("Cập nhật thành công!");
                this.InitGrid();
            }
        }
        
        
    }
}